package com.zretc.controller;

import com.zretc.entity.*;
import com.zretc.service.OrdersService;

import com.zretc.service.ProductShowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;


@Controller
@CrossOrigin
@RequestMapping("/ordersController")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ProductShowService productShowService;

    private static final Logger logger = LoggerFactory.getLogger(OrdersController.class);

    /**
     * 查询所有的订单列表
     * @return
     */
    @RequestMapping("/getAllOrdersList")
    List<Orders> getAllOrdersList() {
       List<Orders> ordersList = ordersService.getAllOrders();
       return ordersList;
    }

    /**
     * 通过订单编号查询所有的订单列表
     * @param orderId  订单编号
     * @return
     */
    @RequestMapping("/getAllOrdersByOrderIdList")
    Orders getAllOrdersByOrderIdList(String orderId) {
        Orders ordersList = ordersService.getOrderbyOrderId(orderId);
        return ordersList;
    }

    /**
     * 通过订单编号删除订单
     * @param orderId  订单编号
     * @return
     */
    @RequestMapping("/deleteByOrderId")
    @ResponseBody
    public boolean deleteByOrderId(String orderId){
        int n = ordersService.deleteByOrder(orderId);
        if (n > 0) {
            return true;
        }
        return false;
    }

    /**
     * 添加订单
     * @return
     */
   @RequestMapping(value = "/insertOrder")
   @ResponseBody
    public boolean insertOrder(String orderId,
                               Integer userId,
                               Float orderTotalPrice,
                               String orderReceiveAddress,
                               String orderReceiveName,
                               String orderReceivePhone,
                               Integer productId) {


       User user = new User();
       user.setUserId(userId);

       Orders orders = new Orders();

       orders.setOrderId(orderId);
       orders.setUser(user);
       orders.setOrderTotalPrice(orderTotalPrice);
       orders.setOrderPayMethod("支付宝支付");
       orders.setOrderStatus(3);
       orders.setOrderReceivePhone(orderReceivePhone);
       orders.setOrderReceiveName(orderReceiveName);
       orders.setOrderReceiveAddress(orderReceiveAddress);

       // 根据商品编号查商品
       Product product = productShowService.findProductById(productId);


       Order order = new Order();
       order.setOrderId(orderId);

       OrderDetail orderDetail = new OrderDetail();
       orderDetail.setProduct(product);
       orderDetail.setOrder(order);
       orderDetail.setStatus(2);
       orderDetail.setCount(1);
       double price = orderDetail.getCount() * product.getProductPrice();

       orderDetail.setTotalPrice(price);

       int n = ordersService.saveOrder(orders);
       ordersService.insertOrderDetail(orderDetail);
        if (n > 0) {
            return true;
        }
        return false;
    }
}
